package com.h2cloud.controller;

import com.h2cloud.entity.Blog;
import com.h2cloud.entity.Collections;
import com.h2cloud.service.BlogService;
import com.h2cloud.service.CollectionsService;
import com.h2cloud.utils.JWTUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * (Collections)表控制层
 *
 * @author makejava
 * @since 2020-09-23 17:56:48
 */
@RestController
@RequestMapping("/collections")
public class CollectionsController {
    @Resource
    private CollectionsService collService;

    @Resource
    private BlogService blogService;


    //获取收藏夹信息
    @GetMapping("/getCollects")
    public Object getCollects(HttpServletRequest request) {

        String token = request.getHeader("Authorization");
        System.out.println(token);
        String userId = JWTUtil.getToken(token).getClaim("id").asString();

        System.out.println("getCollects  " + userId);
        //1.获取收藏夹信息
        List<Collections> coList = collService.queryByUserId(Integer.parseInt(userId));
        //2.保存收藏夹名称
        List<String> tableNameList = new ArrayList();
        //3.保存每个收藏夹的所有 blogInfo
        List tableDataList = new ArrayList();
        //4.已被删除的 blogID
        List<String> nullBLogIDs = new ArrayList();

        //5.处理
        for (int i = 0; i < coList.size(); i++) {
            tableNameList.add(coList.get(i).getName());

            List blogInfoList = new ArrayList<>();
            if (null == (coList.get(i).getBlogids())) {
                System.out.println("该收藏夹暂时无数据");
                blogInfoList.add(null);

            } else {
                String[] ids = coList.get(i).getBlogids().split("M");
                for (String id : ids) {
                    System.out.print(id + "$  ");
                    Blog blogInfo = blogService.queryById(Integer.parseInt(id));
                    if (blogInfo == null) {
                        nullBLogIDs.add(id);
                    } else {
                        if (blogInfo.getStatus() == 1) {
                            blogInfoList.add(blogInfo);
                        }

                    }
                }

                if (!nullBLogIDs.isEmpty()) {
                    System.out.print("清除null 的blog： ");
                    String BlogIDs = coList.get(i).getBlogids();
                    for (String nullBLogID : nullBLogIDs) {
                        System.out.print(nullBLogID + "   ");
                        BlogIDs = BlogIDs.replaceAll(nullBLogID + "M", "");
                    }
                    System.out.println("newBlogs: " + BlogIDs);
                    this.delBlogFromCol(coList.get(i).getId(), BlogIDs);
                    nullBLogIDs = new ArrayList<>();
                }
            }
            System.out.println();
            tableDataList.add(blogInfoList);
        }

        List<Object> AllInfo = new ArrayList<>();

        AllInfo.add(coList);
        AllInfo.add(tableNameList);
        AllInfo.add(tableDataList);

        return AllInfo;
    }

    //删除文件名
    @GetMapping("/deleteTable")
    public Object deleteTable(Integer collectID) {
        System.out.println("/deleteTable");
        System.out.println(collectID);

        collService.deleteById(collectID);

        return null;
    }

    //修改文件名
    @GetMapping("/changeTableName")
    public Object changeTableName(Integer collectID, String newName) {
        System.out.println("/changeTableName");
        System.out.println(collectID + "   " + newName);
        Collections col = new Collections();
        col.setId(collectID);
        col.setName(newName);
        collService.update(col);

        return null;
    }

    //取消收藏
    @GetMapping("/delBlogFromCol")
    public Object delBlogFromCol(Integer collectID, String newBlogIDs) {
        System.out.println("/delBlogFromCol");
        System.out.println(collectID + "   " + "  " + newBlogIDs);

        Collections col = new Collections();
        col.setId(collectID);

        if ("".equals(newBlogIDs)) {
            col.setBlogids(null);
            collService.update2(col);
        } else {

            col.setBlogids(newBlogIDs);
            collService.update(col);
        }

        return null;
    }

    //创建文件夹
    @GetMapping("/createTable")
    public Object createTable(HttpServletRequest request, String name) {
        System.out.println("/createTable");

        String token = request.getHeader("Authorization");
        System.out.println(token);
        String userid = JWTUtil.getToken(token).getClaim("id").asString();

        System.out.println(userid + "   " + name);
        Collections col = new Collections();
        col.setUserid(Integer.parseInt(userid));
        col.setName(name);
        collService.insert(col);

        return null;
    }

    //查找收藏夹
    @GetMapping("/findMyCols")
    public Object findMyCols(HttpServletRequest request) {
        System.out.println("/findMyCols:");
        String token = request.getHeader("Authorization");

        System.out.println(token);
        String userid = JWTUtil.getToken(token).getClaim("id").asString();
        System.out.println(userid);

        System.out.println("/findMyCols:  " + userid);
        return collService.queryByUserId(Integer.parseInt(userid));
    }

    //添加到收藏夹
    @GetMapping("/add2Myfiles")
    public Object add2Myfiles(Integer collectID, String blogids) {
        System.out.println("/add2Myfiles");
        System.out.println(collectID + "   " + blogids);
        Collections col = new Collections();
        col.setId(collectID);
        col.setBlogids(blogids);
        collService.update(col);

        return col;

    }
}
